using 第三次作业.utils;

namespace 第三次作业
{
    public partial class Form1 : Form
    {
        private FileUtil fileUtil;
        public Form1()
        {
            fileUtil = new FileUtil();
            InitializeComponent();
        }

        private void selectBttn_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "C# Files (*.cs)|*.cs";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                filePathArea.Text = openFileDialog.FileName;
                fileUtil = new FileUtil(filePathArea.Text);
            }
        }

        private void sum2Label_Click(object sender, EventArgs e)
        {

        }

        private void sunBttn_Click(object sender, EventArgs e)
        {
            int wordNum = fileUtil.countWords();
            int lineNum = fileUtil.lineNum();

            sumArea.Text = "词数：";
            sumArea.Text += wordNum.ToString();
            sumArea.Text += "\r\n";
            sumArea.Text += "行数：";
            sumArea.Text += lineNum.ToString();
        }

        private void formatBttn_Click(object sender, EventArgs e)
        {
            string[] formattedFile = fileUtil.formatFile();

            string formattedText = "";
            foreach (string line in formattedFile)
            {
                formattedText += line;
                formattedText += "\n";
            }

            int wordNum = FileUtil.countWords(formattedText);
            int lineNum = FileUtil.lineNum(formattedFile);

            sum2Area.Text = "词数：";
            sum2Area.Text += wordNum.ToString();
            sum2Area.Text += "\r\n";
            sum2Area.Text += "行数：";
            sum2Area.Text += lineNum.ToString();

            Dictionary<string, int> wordCount = FileUtil.countWordOccurrences(formattedFile);
            string wordCountText = "";
            foreach (KeyValuePair<string, int> pair in wordCount)
            {
                wordCountText += pair.Key;
                wordCountText += ": ";
                wordCountText += pair.Value.ToString();
                wordCountText += "次";
                wordCountText += "\r\n";
            }
            wordSumArea.ScrollBars = ScrollBars.Vertical;
            wordSumArea.Text = wordCountText;
        }
    }
}